<!DOCTYPE html>
<html class="bg-white" xmlns:th="http://www.thymeleaf.org">
<head th:include="include :: header"></head>
<link rel="stylesheet" th:href="@{/css/admin.css}" media="all" />
<body>
<form id="menuForm" lay-filter="menuForm" class="layui-form model-form">
    <input id="treeId" name="parentId"  type="hidden"/> <!--上级菜单Id-->
    <div class="layui-form-item">
        <label class="layui-form-label">上级菜单</label>
        <div class="layui-input-block">
            <input name="parentName" id="treeName" placeholder="" type="text" class="layui-input inputsur" maxlength="20"  readonly="readonly" lay-verify="required" required/>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">菜单类型</label>
        <div class="layui-input-block">
            <input type="radio" name="menuType" value="M" title="目录" lay-filter="filter"/>
            <input type="radio" name="menuType" value="C" title="菜单" lay-filter="filter"/>
            <input type="radio" name="menuType" value="F" title="按钮" lay-filter="filter"/>
        </div>
    </div>

    <input name="menuId" type="hidden"/> <!--菜单Id-->
    <div class="layui-form-item">
        <label class="layui-form-label">菜单名称</label>
        <div class="layui-input-block">
            <input name="menuName" placeholder="请输入菜单名称" type="text" class="layui-input" maxlength="20"
                   lay-verify="required" required/>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">请求地址</label>
        <div class="layui-input-block">
            <input name="url" id ="url" placeholder="请输入请求地址" type="text" class="layui-input" lay-verify=""
                   required/>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label">权限标识</label>
        <div class="layui-input-block">
            <input name="perms" id="perms" placeholder="请输入权限标识" type="text" class="layui-input" lay-verify=""  required/>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">显示顺序</label>
        <div class="layui-input-block">
            <input name="orderNum" placeholder="请输入显示顺序" type="text" class="layui-input" lay-verify=""  required/>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">图标</label>
        <div class="layui-input-block">
            <input name="icon"  id = "icon" type="text" class="layui-input" lay-verify=""
                   required/>
        </div>
    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">菜单状态</label>
        <div class="layui-input-block">
            <input type="radio" name="status" value="0" title="启用" checked/>
            <input type="radio" name="status" value="1" title="停用"/>
        </div>
    </div>
    <div class="layui-form-item text-right">
        <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        <button class="layui-btn" lay-filter="btnSubmit" lay-submit>保存</button>
    </div>
</form>

<!-- js部分 -->
<div th:include="include::footer"></div>
<script type="text/javascript" th:src="@{/js/common.js}"></script>

<script>
    layui.use(['layer', 'form','admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var admin = layui.admin;
        var prefix = ctx + "system/menu";
        var url = ctx+"system/menu/add";
        // 回显数据
        var menu = admin.getTempData('t_menu');
        if(menu){   //如果是修改数据
            form.val('menuForm', menu);
            url = ctx+"system/menu/edit";
        }
        else{   //新增数据
            var checkNodeInfo =admin.getTempData('t_checkNode');
            $("input[name='parentId']").val(checkNodeInfo.menuId); //上级菜单Id
            $("input[name='parentName']").val(checkNodeInfo.menuName); //上级菜单名称

        }
        //菜单类型选中事件
        form.on('radio(filter)', function(data){
            console.log(data.elem); //得到radio原始DOM对象
            console.log(data.value); //被点击的radio的value值

            var menuType = data.value;
            if (menuType == "M") {
                $("#url").parents(".layui-form-item").hide();
                $("#perms").parents(".layui-form-item").hide();
                $("#icon").parents(".layui-form-item").show();
            } else if (menuType == "C") {
                $("#url").parents(".layui-form-item").show();
                $("#perms").parents(".layui-form-item").show();
                $("#icon").parents(".layui-form-item").hide();
            } else if (menuType == "F") {
                $("#url").parents(".layui-form-item").hide();
                $("#perms").parents(".layui-form-item").show();
                $("#icon").parents(".layui-form-item").hide();
            }
        });

        // 表单提交事件
        form.on('submit(btnSubmit)', function (data) {
            layer.load(2);
            console.log(url);
            $.post(url, data.field, function (data) {
                layer.closeAll('loading');
                if (data.code == 0) {
                    top.layer.msg(data.msg, {icon: 1});
                    admin.putTempData('formOk', true);  // 操作成功刷新表格
                    // 关闭当前iframe弹出层
                    parent.layer.close(parent.layer.getFrameIndex(window.name));
                } else {
                    top.layer.msg(data.msg, {icon: 2});
                }
            });
            return false;
        });


        //菜单管理-新增-选择菜单树
        $("input[name='parentName']").click(function () {
            showSelectTree();
        });

        //跳转到部门树页面
        function showSelectTree() {
            var treeId = $("#treeId").val();
            var menuId = treeId > 0 ? treeId : 1;
            var treeUrl = prefix + "/selectMenuTree/" + menuId;

            layer.open({
                type: 2,
                title: '菜单选择',
                area: ['380px', '455px'],
                content: treeUrl,
                btn: ['确定','关闭'],
                yes: function (index, layero) {
                    var body = layer.getChildFrame('body', index);
                    $("#treeId").val(body.find('#treeId').val());
                    $("#treeName").val(body.find('#treeName').val());
                    layer.close(index);
                }, cancel: function () {
                    return true;
                }
            });
        }

/*        //菜单管理-新增-选择菜单树
        $("input[name='menuType']").click(function () {
            alert(123);
        });*/

    });

</script>
</body>
</html>